package com.appspot.modutajataja;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.ProfilePictureView;

public class OptionActivity extends Activity {
	String[] nameList = { "STARBUCKS", "SONY", "APPLE", "GOOGLE", "MONSTER", "비타500", "NIVEA" };
	String[] recordList = { "00 : 33 : 33", "00 : 34 : 44", "00 : 30 : 45", "00 : 28 : 98", "00 : 29 : 12", "00 : 32 : 01", "00 : 33 : 46" };

	ListView listView;
	Button option_setting;

	private MyAdapter adapter;
	private ProfilePictureView profilePictureView;	
	private Typeface font;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_option);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		Session.openActiveSession(this, true, new Session.StatusCallback() {

			@Override
			public void call(Session session, SessionState state, Exception exception) {
				Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
					@Override
					public void onCompleted(GraphUser user, Response response) {
						if (user != null) {
							profilePictureView.setProfileId(user.getId());
							font = Typeface.createFromAsset(getAssets(), "fonts/shermlock.ttf");

							TextView textName = (TextView) findViewById(R.id.option_id);
							textName.setText("Name : " 
									+ (user.getProperty("username") != null ? user.getProperty("username").toString() 
											: user.getName()));
							textName.setTypeface(font);
							
							TextView textEmail = (TextView) findViewById(R.id.option_email);
							textEmail.setText("Email : " 
									+ (user.getProperty("email") != null ? user.getProperty("email").toString() 
											: user.getProperty("username").toString() + "@facebook.com"));
							textEmail.setTypeface(font);
						}
					}
				});
			}
		});

		WebView webViewInfo = (WebView) findViewById(R.id.optionWebView);
		webViewInfo.getSettings().setJavaScriptEnabled(true);
		webViewInfo.loadUrl(C.tajaAppNewsURL);

		option_setting = (Button) findViewById(R.id.setting);
		option_setting.setOnClickListener(new OptionHandler());

		profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);

		adapter = new MyAdapter(this);
		listView = (ListView) findViewById(R.id.option_ListView);
		listView.setDivider(null);
		listView.setAdapter(adapter);
	}

	class OptionHandler implements OnClickListener {

		@Override
		public void onClick(View v) {
			option_setting.setEnabled(false); // 클릭 무효화
			Handler handler = new Handler();
			handler.postDelayed(new splashhandler(), 1000);

			Dialog dialog = new Dialog(OptionActivity.this);

			// 다이얼로그 바탕화면 제거
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			dialog.setContentView(R.layout.option_setup_dialog);
			dialog.setCanceledOnTouchOutside(true);
			dialog.show();
		}
	}

	class splashhandler implements Runnable {
		public void run() {
			option_setting.setEnabled(true); // 클릭 유효화
		}
	}

	class MyAdapter extends ArrayAdapter {
		Context mc;

		public MyAdapter(Context context) {
			super(context, R.layout.option_list_item, nameList);
			mc = context;
		}

		@Override
		public int getCount() {
			return nameList.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			font = Typeface.createFromAsset(getAssets(), "fonts/cartoonist.ttf");
			
			TextView history = (TextView) findViewById(R.id.option_history_text);
			history.setTypeface(font);

			LayoutInflater inflater = (LayoutInflater) mc.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View root = inflater.inflate(R.layout.option_list_item, null);

			TextView rank = (TextView) root.findViewById(R.id.list_no);
			rank.setText("" + (position + 1));
			
			TextView user_name = (TextView) root.findViewById(R.id.list_username);
			user_name.setText(nameList[position]);
			user_name.setTypeface(font);
			
			
			TextView user_record = (TextView) root.findViewById(R.id.list_timerecord);
			user_record.setText(recordList[position]);
			user_record.setTypeface(font);

			return root;
		} // end of getView method
	} // end of MyAdapter class
	public void HomebackOnClick(View v) {
		startActivity(new Intent(OptionActivity.this, MainActivity.class));
		OptionActivity.this.finish();
	}
	
	@Override
	public void onBackPressed() {

		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
		alt_bld.setMessage("게임을 종료 하시겠습니까?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = alt_bld.create();
		alert.setTitle("게임 종료");
		alert.setIcon(R.drawable.ic_launcher);
		alert.show();
	}
};